/*
 *  $Id$
 *  Copyright [2010] [Panxiaobo] 
 *  Licensed under the Apache License, Version 2.0 (the "License"); 
 *  you may not use this file except in compliance with the License. 
 *  You may obtain a copy of the License at
 *  
 *     http://www.apache.org/licenses/LICENSE-2.0
 *     
 *  Unless required by applicable law or agreed to in writing, software 
 *  distributed under the License is distributed on an "AS IS" BASIS, 
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 *  See the License for the specific language governing permissions and 
 *  limitations under the License. 
 */
package pxb.openid.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import pxb.openid.Forward;
import pxb.openid.ServletUtil;

/**
 * 可以选择转发方式的基础servlet
 * 
 * @author Panxiaobo
 * 
 */
@SuppressWarnings("serial")
public class RedirectServlet extends HttpServlet {
    /**
     * 是否使用表单方式重定向,默认不使用
     */
    private boolean formRedirect = false;

    /**
     * @return the formRedirect
     */
    public boolean isFormRedirect() {
        return formRedirect;
    }

    /**
     * 转发请求
     * 
     * @param req
     * @param resp
     * @param forward
     * @throws ServletException
     * @throws IOException
     */
    protected void redirect(HttpServletRequest req, HttpServletResponse resp, Forward forward) throws ServletException,
            IOException {
        if (isFormRedirect()) {
            ServletUtil.formRedirect(forward, req, resp);
        } else {
            resp.sendRedirect(forward.toURL());
        }
    }

    /**
     * @param formRedirect
     *            the formRedirect to set
     */
    public void setFormRedirect(boolean formRedirect) {
        this.formRedirect = formRedirect;
    }
}
